﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Templates.WinnerCMS6.Comment
{
    /// <summary>
    ///     List 的摘要说明。
    /// </summary>
    public partial class List : PublicPage
    {
        private int _contentId;
        protected Core model;
        private static readonly IP2Location address = new IP2Location();
        private static readonly CoreManage myBll = new CoreManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            this._contentId = int.Parse(Request["ContentId"]);


            this.model = myBll.GetBaseInfo(this._contentId);
            Title = this.model.ContentTitle;
            this.model.Link = StaticFilePath.ContentShowUrl(this.model);
            if (!IsPostBack)
            {
                var flag = myBll.Exists(this._contentId);
                if (!flag)
                {
                    ErrClass.IndexErrRedirect("数据不存在！");
                }

                BindList(1);
            }
        }

        #region 绑定数据

        private void BindList(int pageNo)
        {
            var pp = new PagingParam {PageSize = this.PageNavigation1.PageSize, PageNo = pageNo};

            this.RP_Comment.DataSource = new CommentManage().BindList(this._contentId, true, pp);
            this.RP_Comment.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            this.RP_CommentHot.DataSource = new CommentManage().HotComments(this._contentId);
            this.RP_CommentHot.DataBind();

            this.RP_Week.DataSource = new CoreManage().HotContents(12, this.model.CategoryId, "Week");
            this.RP_Week.DataBind();

            this.RP_Month.DataSource = new CoreManage().HotContents(12, this.model.CategoryId, "Month");
            this.RP_Month.DataBind();
        }

        #endregion

        #region 添加数据

        protected void btnOK_Click(object sender, EventArgs e)
        {
            //CommentInfo model = new CommentInfo();
            //model.ContentId = ContentId;
            //model.UserName = Server.HtmlEncode(txtName.Text);
            //model.Score = byte.Parse(ddlScore.SelectedValue); 
            //model.Content = Server.HtmlEncode(txtContent.Text);
            //model.IP = HttpClient.GetIP();


            //Result resu = new CommentManage().Add(model, ModelId);
            //if (string.IsNullOrEmpty(resu.Errors) == false)
            //{
            //    MsgBox.Message(resu.Errors,this);
            //    return;
            //}

            //if (model.Pass == 0)
            //{
            //    MsgBox.Message("提交成功。\\n待审核以后才能显示出来。", this);
            //}
            //else
            //{ 
            //    BindList(1);
            //    MsgBox.Message("提交成功。", this);
            //}
        }

        #endregion

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected string Address(object ip)
        {
            address.IP = ip.ToString();
            return address.IPLocation();
        }

        protected string GetContent(object objComment)
        {
            var cmt = (Model.Comment) objComment;
            var output = "";
            var quoteList = new CommentManage().GetFatherAndMe(cmt.Path);
            //quoteList.Add(cmt);
            //quoteList.AddRange(cmt.Children);

            for (var i = 0; i < quoteList.Count - 1; i++)
            {
                var quote = quoteList[i];
                output = String.Format(
                    "<div>{0}<h5><em class=\"num\">#{6}</em><a class=\"user-name\" target=\"_blank\" href=\"#\">{1}</a> {8} <em class=\"net\">[{7}]</em></h5><br />{2}<div class=\"bar\"><a href=\"#\" onclick=\"SupportOrOppose({3},true,this);return false;\">支持({4})</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"SupportOrOppose({3},false,this);return false;\">反对({5})</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:ShowReply({3})\">回复</a></div></div>",
                    output, UserName(quote.UserName), quote.Content, quote.Id, quote.Support, quote.Oppose, i + 1,
                    Address(quote.IP), quote.CreateTime);
            }

            var lastComment = quoteList.LastOrDefault();
            if (lastComment == null)
            {
                return cmt.Content;
            }
            // 添加当前引用
            output = String.Format(
                "<div class='comment'>{0}<p>{1}</p></div><div class=\"bar\"><a href=\"#\" onclick=\"SupportOrOppose({3},true,this);return false;\">支持({4})</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"SupportOrOppose({3},false,this);return false;\">反对({5})</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:ShowReply({3})\">回复</a></div>",
                output, lastComment.Content, string.Empty, lastComment.Id, lastComment.Support, lastComment.Oppose);

            return output;
        }

        protected string UserName(object userName)
        {
            if (Convert.ToString(userName).Length == 0)
            {
                return "游客";
            }
            return userName.ToString();
        }
    }
}